package com.px.system.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.px.system.domain.CkkucunDO;
import com.px.system.domain.PandianDO;
import com.px.system.domain.PandianDetailDO;
import com.px.system.service.CkkucunService;
import com.px.system.service.PandianDetailService;
import com.px.system.service.PandianService;
import com.px.common.utils.BDException;
import com.px.common.utils.PageUtils;
import com.px.common.utils.Query;
import com.px.common.utils.R;
import com.px.common.utils.ShiroUtils;

/**
 * 
 * 
 * @author 品讯科技
 * @email zly@hnpinxun.cn
 * @date 2024-08
 */
 
@Controller
@RequestMapping("/system/pandian")
public class PandianController {
	@Autowired
	private PandianService pandianService;
	@Autowired
	private PandianDetailService pandianDetailService;
	
	@Autowired
	private CkkucunService ckkucunService;
	@GetMapping()
	@RequiresPermissions("system:pandian:pandian")
	String Pandian(){
	    return "system/pandian/pandian";
	}
	
	  @GetMapping("/chooseProduct/{trId}")
	    @RequiresPermissions("system:pandian:pandian")
	    public String chooseProduct(@PathVariable("trId") String trId, Model model) {
	        model.addAttribute("trId", trId);
	        return "system/pandian/productList";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:pandian:pandian")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<PandianDO> pandianList = pandianService.list(query);
		int total = pandianService.count(query);
		PageUtils pageUtils = new PageUtils(pandianList, total);
		return pageUtils;
	}
	@ResponseBody
	@GetMapping("/cpList")
	@RequiresPermissions("system:pandian:pandian")
	public PageUtils cpList(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<PandianDO> pandianList = pandianService.list(query);
		int total = pandianService.count(query);
		PageUtils pageUtils = new PageUtils(pandianList, total);
		return pageUtils;
	}
	@GetMapping("/add")
	@RequiresPermissions("system:pandian:add")
	String add(){
	    return "system/pandian/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:pandian:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		PandianDO pandian = pandianService.get(id);
		
		List<PandianDetailDO> detailList=pandianDetailService.getPanDianDetail(id);
		
		model.addAttribute("orderDetails", detailList);
		
		model.addAttribute("pandian", pandian);
	    return "system/pandian/edit";
	}
	@GetMapping("/detail/{id}")
	@RequiresPermissions("system:pandian:pandian")
	String detail(@PathVariable("id") Integer id,Model model){
		PandianDO pandian = pandianService.get(id);
		
		List<PandianDetailDO> detailList=pandianDetailService.getPanDianDetail(id);
		
		model.addAttribute("orderDetails", detailList);
		
		model.addAttribute("pandian", pandian);
	    return "system/pandian/detail";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:pandian:add")
	public R save( PandianDO pandian){
		
		SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmss");//设置日期格式
		String name="PD"+df.format(new Date());
		pandian.setPdCode(name);
		for (PandianDetailDO pandianDO : pandian.getDetails()) {
			  if (pandianDO.getPdNumber() == null) {
                throw new BDException("盘点数量未填写！");
            }
		};
		if (pandian.getPdName()==null||pandian.getPdName().equals("")) {
			 throw new BDException("盘点人员未选择！");
		}
		if (pandian.getPdTime()==null||pandian.getPdTime().equals("")) {
			 throw new BDException("盘点时间未选择！");
		}
		
		
		
		if(pandianService.save(pandian)>0){
			PandianDetailDO pandianDetail3=new PandianDetailDO();
			for (PandianDetailDO pandianDO2 : pandian.getDetails()) {
				pandianDetail3.setPdId(pandian.getId());
				pandianDetail3.setCpId(pandianDO2.getCpId());
				pandianDetail3.setCpName(pandianDO2.getCpName());
				pandianDetail3.setCpType(pandianDO2.getCpType());
				pandianDetail3.setUtil(pandianDO2.getUtil());
				pandianDetail3.setCpNumber(pandianDO2.getCpNumber());
				pandianDetail3.setPdNumber(pandianDO2.getPdNumber());
				pandianDetail3.setDifference(pandianDO2.getDifference());
				pandianDetail3.setDifferenceName(pandianDO2.getDifferenceName());
				pandianDetailService.save(pandianDetail3);
			};
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:pandian:edit")
	public R update( PandianDO pandian){
		
		for (PandianDetailDO pandianDO : pandian.getDetails()) {
			  if (pandianDO.getPdNumber() == null) {
              throw new BDException("盘点数量未填写！");
          }
		};
		
		
		pandianService.update(pandian);
		
		int num=pandianDetailService.removeDetail(pandian.getId());
		PandianDetailDO pandianDetail3=new PandianDetailDO();
		for (PandianDetailDO pandianDO2 : pandian.getDetails()) {
			pandianDetail3.setPdId(pandian.getId());
			pandianDetail3.setCpId(pandianDO2.getCpId());
			pandianDetail3.setCpName(pandianDO2.getCpName());
			pandianDetail3.setCpType(pandianDO2.getCpType());
			pandianDetail3.setUtil(pandianDO2.getUtil());
			pandianDetail3.setCpNumber(pandianDO2.getCpNumber());
			pandianDetail3.setPdNumber(pandianDO2.getPdNumber());
			pandianDetail3.setDifference(pandianDO2.getDifference());
			pandianDetail3.setDifferenceName(pandianDO2.getDifferenceName());
			pandianDetailService.save(pandianDetail3);
		};
		
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:pandian:remove")
	public R remove( Integer id){
		if(pandianService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:pandian:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		pandianService.batchRemove(ids);
		return R.ok();
	}
	
	/**
	 * 审核
	 */
	@PostMapping( "/check")
	@ResponseBody
	@RequiresPermissions("system:pandian:check")
	public R check(@RequestParam("ids[]") Integer[] ids){
		PandianDO pandian=new PandianDO();
		 for (Integer integer : ids) {
			 pandian.setId(integer);
			 pandian.setCheck(2);
			 CkkucunDO kucun=new CkkucunDO();
			 List<PandianDetailDO> detailList=pandianDetailService.getPanDianDetail(integer);
				
				for (PandianDetailDO pandianDetail : detailList) {
					
					kucun.setId(pandianDetail.getCpId());
					kucun.setAllnumber(pandianDetail.getPdNumber());
					ckkucunService.update2(kucun);
				}
				
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
				
				pandian.setCheckId(ShiroUtils.getUser().getUserId().intValue());
				pandian.setCheckName(ShiroUtils.getUser().getUsername());
				pandian.setCheckTime(df.format(new Date()));
				pandianService.update(pandian);	
				
		}
		return R.ok();
	}
	/**
	 * 审核不通过
	 */
	@PostMapping( "/checkFalse")
	@ResponseBody
	@RequiresPermissions("system:pandian:check")
	public R checkFalse(@RequestParam("ids[]") Integer[] ids){
		PandianDO pandian=new PandianDO();
		 for (Integer integer : ids) {
			 pandian.setId(integer);
			 pandian.setCheck(3);
			 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			 pandian.setCheckId(ShiroUtils.getUser().getUserId().intValue());
			 pandian.setCheckName(ShiroUtils.getUser().getUsername());
			 pandian.setCheckTime(df.format(new Date()));
				pandianService.update(pandian);		
		}
		return R.ok();
	}
	
}
